package com.lovo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Test02 {
    public Connection getConnection(){
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root","lovo");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(Connection con, Statement statement, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con,Statement statement){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void insert(Student student){
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "insert into t_student(s_name,s_age,s_gender) values(?,?,?)";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,student.getName());
            statement.setObject(2,student.getAge());
            statement.setObject(3,student.getGender());

            int row = statement.executeUpdate();
            System.out.println("row:" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement);
        }
    }
    public List query(String name){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        List list = new ArrayList();
        String sql = "select * from t_student where s_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,name);
            rs = statement.executeQuery();
            while(rs.next()){
                Student student = new Student();
                student.setId(rs.getInt("s_id"));
                student.setName(rs.getString("s_name"));
                student.setGender(rs.getString("s_gender"));
                student.setAge(rs.getInt("s_age"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return list;
    }

    public static void main(String[] args) {
        Test02 test02 = new Test02();
        Student student = new Student("赵六","男",20);
//        test02.insert(student);
        System.out.println(test02.query("张飞' or '1'='1"));
    }
}
